package pl.softech.gpw.candles.patterns.impl2;

import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import pl.softech.gpw.candles.Candle;
import pl.softech.gpw.candles.editor.CandlestickWidget;
import pl.softech.gpw.candles.editor.xml.XmlDefinitionLoader;
import pl.softech.gpw.candles.patterns.CandlePattern;
import pl.softech.gpw.candles.patterns.ICandlePattern;
import pl.softech.gpw.candles.patterns.ICandlePatternMetaData;
import pl.softech.gpw.model.FInstrument;

public class XmlCandlePattern implements ICandlePattern {

    private final ICandlePatternMetaData metaData;
    private final Candle[] pattern;

    private static PatternRecognizer patternRecognizer = new PatternRecognizer();

    private XmlCandlePattern(ICandlePatternMetaData metaData, Candle[] pattern) {
        this.metaData = metaData;
        this.pattern = pattern;
    }

    @Override
    public Type getType() {
        return metaData.getType();
    }

    @Override
    public int getBarCount() {
        return pattern.length;
    }

    @Override
    public Direction getDirection() {
        return metaData.getDirection();
    }

    @Override
    public Reliability getReliability() {
        return metaData.getReliability();
    }

    @Override
    public boolean test(FInstrument[] finstruments) {

        return patternRecognizer.test(pattern, finstruments);

    }

    private static Candle[] toCandles(CandlestickWidget[] widgets) {
        Candle[] candles = new Candle[widgets.length];
        for (int i = 0; i < widgets.length; i++) {
            candles[i] = widgets[i].getCandlestick();
        }
        return candles;
    }

    public static XmlCandlePattern create(String fileName) {

        XmlCandlePattern pattern = null;

        try {

            File file = new File(XmlCandlePattern.class.getResource(fileName).getFile());
            XmlDefinitionLoader loader = new XmlDefinitionLoader();
            loader.load(file);
            pattern = new XmlCandlePattern(loader.getMetaData(), toCandles(loader.getCandlestickWidgets()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return pattern;
    }

    public static void main(String[] args) {

        XmlCandlePattern pattern = XmlCandlePattern.create("bullish-engulfing-pattern.xml");

        FInstrument[] instruments = new FInstrument[pattern.pattern.length];
        for (int i = 0; i < instruments.length; i++) {
            instruments[i] = pattern.pattern[i].instrument();
        }

        System.out.println((CandlePattern.BULLISH_ENGULFING.test(instruments) ? "OK" : "ERROR") + "\tIS PATTERN BULLISH_ENGULFING");
        System.out.println((CandlePattern.BULLISH_ENGULFING_XML.test(instruments) ? "OK" : "ERROR") + "\tIS PATTERN BULLISH_ENGULFING_XML");

        List<FInstrument> tmp = new LinkedList<FInstrument>(Arrays.asList(instruments));
        tmp.addAll(Arrays.asList(instruments));
        tmp.addAll(Arrays.asList(instruments));

        instruments = tmp.toArray(new FInstrument[0]);
        System.out.println((CandlePattern.BULLISH_RISING_TREE_METHODS_XML.test(instruments) ? "ERROR" : "OK") + "\tIS NOT BULLISH_RISING_TREE_METHODS_XML");
        System.out.println((CandlePattern.BULLISH_RISING_TREE_METHODS.test(instruments) ? "ERROR" : "OK") + "\tIS NOT BULLISH_RISING_TREE_METHODS");

        pattern = XmlCandlePattern.create("bullish-rising-three-methods-pattern.xml");

        instruments = new FInstrument[pattern.pattern.length];
        for (int i = 0; i < instruments.length; i++) {
            instruments[i] = pattern.pattern[i].instrument();
        }

        System.out.println((CandlePattern.BULLISH_RISING_TREE_METHODS.test(instruments) ? "OK" : "ERROR") + "\tIS BULLISH_RISING_TREE_METHODS");
        System.out.println((CandlePattern.BULLISH_RISING_TREE_METHODS_XML.test(instruments) ? "OK" : "ERROR") + "\tIS BULLISH_RISING_TREE_METHODS_XML");
    }

}
